package b4.view.component.admin
{
	import b4.config.Config;
	import b4.config.LayerConfig;
	import b4.config.LayerItemConfig;
	import b4.config.TownConfig;
	
	import flash.events.Event;
	
	import org.aswing.*;

	public class PaintDialog extends JFrame
	{
		private var townIndex:int;
		public var townCombo:JComboBox;
		public var loadTownButton:JButton;
		public var editModeButton:JToggleButton;
		public var loadLayerButton:JButton; 
		public var layerCombo:JComboBox;
		public var infoLable:JLabel;
		private var itemCombo:JComboBox;
		private var newItemCombo:JComboBox;
		private var penSizeComb:JComboBox;
		private var layerItemEditPane:LayerItemEditPane;
		
		public function PaintDialog()
		{
			super();
//			setResizable(false);
			var wholePane:JPanel = new JPanel();//new BoxLayout(BoxLayout.Y_AXIS));
			//上部
			var topPane:JPanel = new JPanel();
			//地图
			var townNames:Array = new Array();
			for (var indexStr:String in Config.townConfigs)
			{
				var townConfig:TownConfig = Config.townConfigs[indexStr];
				townNames.push(townConfig.index + " " + townConfig.name);
			}
			townCombo = new JComboBox(townNames);
			townCombo.setPreferredWidth(120);
			topPane.append(townCombo);
			loadTownButton = new JButton("加载地图");
			topPane.append(loadTownButton);
			//层
			layerCombo = new JComboBox([]);
			layerCombo.setPreferredWidth(120);
			topPane.append(layerCombo);
			loadLayerButton = new JButton("加载层");
			topPane.append(loadLayerButton);
			//
			itemCombo = new JComboBox([]);
			itemCombo.setSelectedIndex(0);
			itemCombo.setPreferredWidth(120);
			itemCombo.addActionListener(onPenItemSelect);
			topPane.append(itemCombo);
			//笔粗细
			penSizeComb = new JComboBox(["笔1", "笔2", "笔3"]);
			penSizeComb.setPreferredWidth(100);
			penSizeComb.addActionListener(onPenSizeSelect);
			penSizeComb.setSelectedIndex(2);
			topPane.append(penSizeComb);
			
			newItemCombo = new JComboBox(LayerItemConfig.NAMES);
			newItemCombo.setSelectedIndex(0);
			newItemCombo.setPreferredWidth(120);
			topPane.append(newItemCombo);
			var newItemButton:JButton = new JButton("创建新类型");
			newItemButton.addActionListener(onNewItemButtonClick);
			topPane.append(newItemButton);
			var exportButton:JButton = new JButton("导出地图");
			exportButton.addActionListener(onExportButtonClick);
			topPane.append(exportButton);
			editModeButton = new JToggleButton("编辑模式");
			topPane.append(editModeButton);
			infoLable = new JLabel();
			infoLable.setPreferredWidth(120);
			topPane.append(infoLable);
			
			topPane.setPreferredWidth(400);
			topPane.setPreferredHeight(80);
			wholePane.append(topPane);
			//下部
			layerItemEditPane = new LayerItemEditPane();
			layerItemEditPane.setPreferredWidth(600);
			layerItemEditPane.setPreferredHeight(260);
			layerItemEditPane.y = 100;
			wholePane.append(layerItemEditPane);
			this.setContentPane(wholePane);
			this.setComBoundsXYWH(100, 100, 620, 400);
		}
		
		public function loadTown(townIndex:int, layerIndex:int):void
		{
			this.townIndex = townIndex;
			
			var townConfig:TownConfig = Config.townConfigs[townIndex];
			townCombo.setSelectedItem(townConfig.index + " " + townConfig.name);
			var layerArr:Array = new Array();
			for (var i:int = 0; i < townConfig.layers.length; i++)
			{
				layerArr.push(i + " 第" + (i + 1) + "层");
			}
			layerCombo.setListData(layerArr);
			layerCombo.setSelectedIndex(layerIndex);
			var layerConfig:LayerConfig = townConfig.layers[layerIndex];
			var itemArr:Array = ["无"];
			for (i = 0; i < layerConfig.items.length; i++)
			{
				var itemConfig:LayerItemConfig = layerConfig.items[i];
				itemArr.push(LayerItemConfig.NAMES[itemConfig.type] + i);
			}
			itemCombo.setListData(itemArr);
			itemCombo.setSelectedIndex(0);
		}
		
		private function onExportButtonClick(event:Event):void
		{
			var frame:JFrame = new JFrame(AsWingUtils.getPopupAncestor(this), "地图导出");
			var pane:JScrollPane = new JScrollPane();
			var townConfig:TownConfig = Config.townConfigs[townIndex];
			if (townConfig != null)
			{
				var layerIndex:int = layerCombo.getSelectedIndex();
				var layerConfig:LayerConfig = townConfig.layers[layerIndex];
				if (layerConfig != null)
				{
					var textArea:JTextArea = new JTextArea();
					for (var j:int = 0; j < layerConfig.items.length; j++)
					{
						var itemConfig:LayerItemConfig = layerConfig.items[j];
						if (itemConfig.type != LayerItemConfig.TYPE_EMPTY)
						{
							textArea.appendText(itemConfig.serializeExport() + "\n");
						}
					}
					pane.append(textArea);
					frame.setContentPane(pane);
					frame.setComBoundsXYWH(100, 100, 400, 240);
					frame.show();
				}
			}
		}
		
		private function onNewItemButtonClick(event:Event):void
		{
			var townConfig:TownConfig = Config.townConfigs[townIndex];
			var layerIndex:int = layerCombo.getSelectedIndex();
			var type:int = newItemCombo.getSelectedIndex();
			var layerConfig:LayerConfig = townConfig.layers[layerIndex];
			var itemConfig:LayerItemConfig = LayerConfig.createLayerItem(type);
			itemConfig.type = type;
			itemConfig.index = layerConfig.items.length; 
			layerConfig.items.push(itemConfig);
			var itemArr:Array = ["无"];
			for (var i:int = 0; i < layerConfig.items.length; i++)
			{
				itemConfig = layerConfig.items[i];
				itemArr.push(LayerItemConfig.NAMES[itemConfig.type] + i);
			}
			itemCombo.setListData(itemArr);
			itemCombo.setSelectedIndex(itemArr.length - 1);
		}
		
		private function onPenSizeSelect(event:Event):void
		{
			Painter.penSize = penSizeComb.getSelectedIndex() + 1;
		}
		
		private function onPenItemSelect(event:Event):void
		{
			var townConfig:TownConfig = Config.townConfigs[townIndex];
			var layerIndex:int = layerCombo.getSelectedIndex();
			var layerConfig:LayerConfig = townConfig.layers[layerIndex];
			var itemIndex:int = itemCombo.getSelectedIndex();
			if (itemIndex > 0)
			{
				var itemConfig:LayerItemConfig = layerConfig.items[itemIndex - 1];
				layerItemEditPane.loadLayerItem(itemConfig);
				Painter.penMode = itemConfig;
			}
			else
			{
				Painter.penMode = null;
			}
		}
		
	}
}